
package com.platform.modules.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.platform.common.utils.Query;
import com.platform.modules.mall.dao.MallBranchDao;
import com.platform.modules.mall.entity.MallBranchEntity;
import com.platform.modules.mall.location.utils.LocationUtil;
import com.platform.modules.mall.service.MallBranchService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 网点Service实现类
 *
 * @author
 * @date 2021-04-20 09:59:04
 */
@Service("mallBranchService")
public class MallBranchServiceImpl extends ServiceImpl<MallBranchDao, MallBranchEntity> implements MallBranchService {

    @Override
    public Boolean branchExists(String name) {
        return SqlHelper.retBool(baseMapper.branchExists(name));
    }

    @Override
    public Integer updateBanchRelation() {
        return baseMapper.updateBanchRelation();
    }

    @Override
    public List<MallBranchEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<MallBranchEntity> page = new Query<MallBranchEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMallBranchPage(page, params));
    }

    private void branchDataChange(MallBranchEntity mallBranch){
        if(StringUtils.isNotBlank(mallBranch.getLocation())){
            mallBranch.setLocation("ST_GeomFromText('POINT(" + mallBranch.getLocation() + ")',4326)");
        }else{
            mallBranch.setLocation("ST_GeomFromText('POINT(0 0)',4326)");
        }
    }

    @Override
    public boolean add(MallBranchEntity mallBranch) throws Exception {
        // 设置经纬度
        branchDataChange(mallBranch);
        return this.save(mallBranch);
    }

    @Override
    public boolean update(MallBranchEntity mallBranch) {
        branchDataChange(mallBranch);
        return this.updateById(mallBranch);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public IPage getNearbySimple(Map<String, Object> params) {
        LocationUtil.initialNearbyParams(params);
        //排序
        params.put("sidx", "distance");
        params.put("asc", true);
        Page<Map<String,Object>> page = new Query<Map<String,Object>>(params).getPage();
        page.setRecords(baseMapper.getNearbySimple(page, params));
        return page;
    }
}
